package adapters;

import interfaces.IRegister;

import java.io.IOException;
import java.net.MalformedURLException;

import javax.xml.parsers.ParserConfigurationException;

import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class RegisterAdapter extends Adapter {
	private String username;
	private String password;
	private String email;
	private String identityNo;
	
	@Override
	public String callWebService(String xmlString) throws ParserConfigurationException, SAXException, IOException {
		NodeList paramNodeList = getParamNodeList(xmlString);
		for (int i=0;i<paramNodeList.getLength();i++){
			Element paramElement = (Element)paramNodeList.item(i);
			String name = paramElement.getAttribute("name");
			String value = paramElement.getAttribute("value");
			if (name.equals("username")){
				username = value;
			}else if (name.equals("password")){
				password = value;
			}else if (name.equals("email")){
				email = value;
			}else if (name.equals("identityNo")){
				identityNo = value;
			}
		}
		Service serviceModel = new ObjectServiceFactory().create(IRegister.class);
        String serviceUrl = "http://localhost:8080/MyProj/services/RegisterService";
        IRegister client = null;
        try {
            client = (IRegister) xFireProxyfactory.create(serviceModel, serviceUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        String serviceResponse = "";
        serviceResponse += "<webService name='Register'>";
        serviceResponse += client.register(username, password, email, identityNo);
        serviceResponse += "</webService>";
        return serviceResponse; 
	}
}
